//
//  SLDemo_BaseNavController.m
//  Objective-C
//
//  Created by S_Line on 2018/5/4.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLDemo_BaseNavController.h"

#import "AppDelegate.h"
#import "UIDevice+SLCategory.h"

#import "UIImage+Property.h"

#import "ReactiveObjC.h"

@interface SLDemo_BaseNavController ()<UITextFieldDelegate>

@property (nonatomic, weak) UIScrollView *scrollView;

@property (nonatomic, assign) NSInteger leftRightValue;

@end

@implementation SLDemo_BaseNavController

- (void)initNavBar {
    self.navTitle = @"导航栏演示";
    
    self.leftTitle = @"返回";
    self.leftColor = [UIColor redColor];
    self.rightImage = [UIImage imageNamed:@"nav_menu"];
    self.rightColor = [UIColor blueColor];
    
    self.rightImage.name = @"nav_menu";
    NSLog(@"self.rightImage.name: %@", self.rightImage.name);
    
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"付过天汇成长混合\n" attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:17], NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#000000"]}]];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"000100 | 中风险" attributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:12], NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#00ff00"]}]];
    self.attTitle = attString;
    
    self.navColor = [UIColor colorWithHexString:@"#282D41"];
    self.statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initView];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    //允许转成横屏
    appDelegate.allowRotation = false;
    //调用横屏代码
    [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)initView {
    self.view.backgroundColor = [UIColor yellowColor];
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    //允许转成横屏
    appDelegate.allowRotation = YES;
    //调用横屏代码
    [UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(kNavigationBarHeight);
    }];
    
    UIView *widthView = [[UIView alloc] init];
    [scrollView addSubview:widthView];
    [widthView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0);
        make.width.equalTo(scrollView.mas_width);
    }];
    
    UILabel *statusBarTitleLabel = [self renderLabelWithText:@"状态栏控制"];
    [scrollView addSubview:statusBarTitleLabel];
    [statusBarTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
    }];
    statusBarTitleLabel.preferredMaxLayoutWidth = 100;      // 设置Label的最大预设宽度,一般和width相等
    statusBarTitleLabel.minimumScaleFactor = 0.5;           // label字体的最大缩小比例, 如果你设的字体是18, 当现实不下时字号最小缩小为9
    
    UISegmentedControl *statusBarControl = [[UISegmentedControl alloc] initWithItems:@[@"Default", @"LightContent"]];
    [statusBarControl addTarget:self action:@selector(statusBarControlClick:) forControlEvents:UIControlEventValueChanged];
    statusBarControl.selectedSegmentIndex = 1;
    [scrollView addSubview:statusBarControl];
    [statusBarControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(statusBarTitleLabel.mas_bottom).mas_offset(8);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(30);
    }];
    
    UILabel *leftRightControlTitleLabel = [self renderLabelWithText:@"左右按钮控制"];
    [scrollView addSubview:leftRightControlTitleLabel];
    [leftRightControlTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(statusBarControl.mas_bottom).mas_offset(8);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
    }];
    
    UISegmentedControl *leftRightControl = [[UISegmentedControl alloc] initWithItems:@[@"左", @"右"]];
    [leftRightControl addTarget:self action:@selector(leftRightControlClick:) forControlEvents:UIControlEventValueChanged];
    leftRightControl.selectedSegmentIndex = 0;
    [scrollView addSubview:leftRightControl];
    [leftRightControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(leftRightControlTitleLabel.mas_bottom).mas_offset(8);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(30);
    }];
    
    UISegmentedControl *leftImageControl = [[UISegmentedControl alloc] initWithItems:@[@"nil", [UIImage imageNamed:@"nav_back"], [UIImage imageNamed:@"nav_menu"], [UIImage imageNamed:@"nav_share"], [UIImage imageNamed:@"nav_comment"]]];
    leftImageControl.selectedSegmentIndex = 0;
    [leftImageControl addTarget:self action:@selector(leftImageControlClick:) forControlEvents:UIControlEventValueChanged];
    [scrollView addSubview:leftImageControl];
    [leftImageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(leftRightControl.mas_bottom).mas_offset(8);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(30);
    }];
    
    UITextField *leftRightNameTextField = [[UITextField alloc] init];
    leftRightNameTextField.delegate = self;
    [scrollView addSubview:leftRightNameTextField];
    leftRightNameTextField.borderStyle = UITextBorderStyleRoundedRect;
    [leftRightNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(leftImageControl.mas_bottom).mas_offset(8);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
    }];
    @weakify(self);
    [[leftRightNameTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        @strongify(self);
        if (self.leftRightValue == 0) {
            self.leftTitle = x;
        } else if (self.leftRightValue == 1) {
            self.rightTitle = x;
        }
    }];
}

- (UILabel *)renderLabelWithText:(NSString *)text {
    UILabel *label = [[UILabel alloc] init];
    
    label.font = [UIFont systemFont13];
    label.text = text;
    
    return label;
}

#pragma mark ----- UITextField代理方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *oldString = textField.text ? textField.text : @"";
    NSString *newString = [oldString stringByReplacingCharactersInRange:range withString:string];
    if (self.leftRightValue == 0) {
        self.leftTitle = newString;
    } else if (self.leftRightValue == 1) {
        self.rightTitle = newString;
    }
    return true;
}

#pragma mark ----- UISegmentControl点击事件
- (void)statusBarControlClick:(UISegmentedControl *)segmentedControl {
    NSLog(@"selectedSegmentIndex: %ld", segmentedControl.selectedSegmentIndex);
    NSInteger selectedSegmentIndex = segmentedControl.selectedSegmentIndex;
    
    if (selectedSegmentIndex == 0) {
        self.statusBarStyle = UIStatusBarStyleDefault;
    } else if (selectedSegmentIndex == 1) {
        self.statusBarStyle = UIStatusBarStyleLightContent;
    }
}

- (void)leftRightControlClick:(UISegmentedControl *)segmentedControl {
    NSInteger selectedSegmentIndex = segmentedControl.selectedSegmentIndex;
    
    self.leftRightValue = selectedSegmentIndex;
}

- (void)leftImageControlClick:(UISegmentedControl *)segmentedControl {
    NSInteger selectedSegmentIndex = segmentedControl.selectedSegmentIndex;
    
    UIImage *selectImage = nil;
    if (selectedSegmentIndex == 1) {
        selectImage = [UIImage imageNamed:@"nav_back"];
    } else if (selectedSegmentIndex == 2) {
        selectImage = [UIImage imageNamed:@"nav_menu"];
    } else if (selectedSegmentIndex == 3) {
        selectImage = [UIImage imageNamed:@"nav_share"];
    } else if (selectedSegmentIndex == 4) {
        selectImage = [UIImage imageNamed:@"nav_comment"];
    }
    
    if (self.leftRightValue == 0) {
        self.leftImage = selectImage;
    } else if (self.leftRightValue == 1) {
        self.rightImage = selectImage;
    }
}

@end
